﻿using System;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using System.Web.UI;

using System.Web.UI.HtmlControls;

namespace VPLink.WebParts
{
    public class XmlFileURLToolPart : ToolPart
    {
        private HtmlInputText tbxXmlFileURL;

        /// <summary>
        /// Constructor for the class.
        /// </summary>        
        public XmlFileURLToolPart()
        {
            this.Title = "URL du fichier xml :";
            this.PreRender += new EventHandler(XmlFileURLToolPart_PreRender);
        }

        /// <summary>
        /// Create dropdownlist to display Announcement lists
        /// </summary>
        override protected void CreateChildControls()
        {
            try
            {
                Label lblTitre = new Label();
                lblTitre.Text = "Entrez l'URL du fichier Xml :";
                Controls.Add(lblTitre);

                LiteralControl lit2 = new LiteralControl("<BR>&nbsp;&nbsp;&nbsp;");
                Controls.Add(lit2);
                tbxXmlFileURL = new HtmlInputText();
                tbxXmlFileURL.Style.Add("width", "165px");
                Controls.Add(tbxXmlFileURL);
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        ///	<summary>
        ///	This method is called by the ToolPane object to apply property changes to the selected Web Part. 
        ///	</summary>
        override public void ApplyChanges()
        {
            // Apply property values here.
            IXmlFileLinkedWebPart currentWebPart = ParentToolPane.SelectedWebPart as IXmlFileLinkedWebPart;
            if (currentWebPart != null)
            {
                currentWebPart.XmlFileURL = this.tbxXmlFileURL.Value;
            }
        }

        ///	<summary>
        ///	Initializes ToolPart values. 
        ///	</summary>
        private void XmlFileURLToolPart_PreRender(object sender, EventArgs ea)
        {
            try
            {
                IXmlFileLinkedWebPart currentWebPart = ParentToolPane.SelectedWebPart as IXmlFileLinkedWebPart;
                if (currentWebPart != null)
                {
                    string xmlFileURL = currentWebPart.XmlFileURL;
                    this.tbxXmlFileURL.Value = xmlFileURL;
                }
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }
    }
}
